package gw.com.android.ui.positions;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.gwtsz.gts2.hx.R;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import gw.com.android.app.AppContances;
import gw.com.android.app.AppMain;
import gw.com.android.model.DataManager;
import gw.com.android.utils.ColorThemeUtil;
import gw.com.jni.library.terminal.GTSConst;
import www.com.library.model.DataItemDetail;
import www.com.library.model.DataItemResult;
import www.com.library.util.StringFormatter;

/**
 * Created by reeta.zou on 2016/7/7.
 * 当日盈亏界面适配器
 */
public class ProfitAdapter extends RecylerListAdapter {

    private DataItemResult mList;
    private DataItemDetail mItem;

    /**
     * 初始化界面数据
     *
     * @param context
     */
    public ProfitAdapter(Context context, RecyclerView mView) {
        super(context, mView);
        mList = new DataItemResult();
        mList.appendItems(DataManager.instance().getSortOrderList(DataManager.instance().mCloseList));
        notifyDataChanged();
    }

    @Override
    public void refreshData() {
        mList.clear();
        mList.appendItems(DataManager.instance().getSortOrderList(DataManager.instance().mCloseList));
        notifyDataChanged();
    }

    @Override
    public int getItemCount() {
        if(null != mList){
            return mList.getDataCount();
        }
        return 0;
    }

    public DataItemDetail getItem(int position) {
        if (position < 0 || position >= mList.getDataCount()) {
            return null;
        }

        if(null !=  mList && null != mList.getItem(position)){
            return mList.getItem(position);
        }

        return null;
    }

    public void updateViews(RecyclerView.ViewHolder viewholder, int position) {
        mItem = getItem(position);
        ListItemView mItemView = (ListItemView) viewholder;
        if(null != mItem){
            updateViews(mItemView);
        }
    }

    @Override
    public void updateViews(RecyclerView.ViewHolder viewholder, List payloads) {

    }

    /**
     * 刷新界面
     * @param mItemView
     */
    private void updateViews(ListItemView mItemView){
        mItemView.prdnameV.setText(DataManager.instance().getPrdName(mItem));
        // 开仓价
        mItemView.donePrice.setText(AppMain.getAppString(R.string.order_item_open_price, mItem.getString(GTSConst.JSON_KEY_OPENPRICE)));
        // 平仓价
        mItemView.closePrice.setText(AppMain.getAppString(R.string.order_item_close_price, mItem.getString(GTSConst.JSON_KEY_CLOSEPRICE)));
        mItemView.timeV.setText(StringFormatter.instance().secToTime(mItem.getInt(GTSConst.JSON_KEY_CLOSETIME_)));

        mItemView.swapV.setText(AppMain.getAppString(R.string.order_item_title_swap) + mItem.getString(GTSConst.JSON_KEY_INTEREST));
        mItemView.commissionV.setText(AppMain.getAppString(R.string.order_item_title_commission) + mItem.getString(GTSConst.JSON_KEY_COMMISSION));
        /**产品编号*/
        mItemView.orderIdV.setText("#" + String.valueOf(mItem.getInt(GTSConst.JSON_KEY_ID)));
        // 买卖类型和手数
        m_spannableTitle.reset();
        if (mItem.getInt(GTSConst.JSON_KEY_DIRECTION) == AppContances.TRADE_TYPE_BUY) {
            mItemView.lotV.setText(AppMain.getAppString(R.string.order_trade_buy_lot, mItem.getString(GTSConst.JSON_KEY_VOLUME)));
        } else {
            mItemView.lotV.setText(AppMain.getAppString(R.string.order_trade_sell_lot, mItem.getString(GTSConst.JSON_KEY_VOLUME)));
        }

        mItemView.profitV.setText(mItem.getString(GTSConst.JSON_KEY_PROFIT));
        ColorThemeUtil.instance().setPriceColor(mItemView.profitV, mItem.getInt(GTSConst.JSON_KEY_PROFITSTATE));
    }

    @Override
    public int getLayoutID() {
        return R.layout.list_item_profit;
    }

    @Override
    public RecyclerView.ViewHolder getViewHolder(View v, int type) {
        return new ListItemView(v);
    }

    public class ListItemView extends RecyclerView.ViewHolder{
        /** 产品名称 */
        @BindView(R.id.trade_prd_name)
        public TextView prdnameV;
        /** 时间 */
        @BindView(R.id.trade_time)
        public TextView timeV;
        /** 手数 */
        @BindView(R.id.trade_type)
        public TextView lotV;
        /** 开仓价 */
        @BindView(R.id.trade_open_price)
        public TextView donePrice;
        /** 平仓价 */
        @BindView(R.id.trade_close_price)
        public TextView closePrice;
        /** 净盈亏 */
        @BindView(R.id.trade_profit)
        public TextView profitV;
        /**手续费*/
        @BindView(R.id.trade_Commission)
        public TextView commissionV;
        /**利息*/
        @BindView(R.id.trade_Swap)
        public TextView swapV;
        /**产品名称*/
        @BindView(R.id.trade_order_id)
        public TextView orderIdV;

        public ListItemView(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
